package com.lzxzjy.web.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebFilter(urlPatterns = {"/admin/*", "/announcement/*", "/book/*", "/borrowInfo/*", "/message/*", "/reader/*", "/adminIndex.html", "/readerIndex.html"})
public class BaseFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void destroy() {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        HttpSession session = request.getSession();

        if (request.getRequestURL().toString().contains("/readerIndex.html")) {
            if (session.getAttribute("reader") != null) {
                filterChain.doFilter(servletRequest, servletResponse);
                return;
            }
            response.sendRedirect("http://localhost:9090/BookManage/readerLogin.html#-1");
        } else if (request.getRequestURL().toString().contains("/selectAllAnnouncement") || request.getRequestURL().toString().contains("/getUser") || request.getRequestURL().toString().contains("/selectBookByPageAndTerms") || request.getRequestURL().toString().contains("/selectAllBookType") || request.getRequestURL().toString().contains("/selectBorrowInfoByPageAndTerms") || request.getRequestURL().toString().contains("/borrowBooks") || request.getRequestURL().toString().contains("/borrowBooks") || request.getRequestURL().toString().contains("/lostBooks") || request.getRequestURL().toString().contains("/lostBook") || request.getRequestURL().toString().contains("/returnBooks") || request.getRequestURL().toString().contains("/returnBook") || request.getRequestURL().toString().contains("/selectReaderByPageAndTerms") || request.getRequestURL().toString().contains("/updateReaderById") || request.getRequestURL().toString().contains("/selectAnnouncementByPageAndTerms") || request.getRequestURL().toString().contains("/insertMessage") || request.getRequestURL().toString().contains("/selectMessageByPageAndTerms") || request.getRequestURL().toString().contains("/readMessageByReaderId") || request.getRequestURL().toString().contains("/readMessageByMessageId") || request.getRequestURL().toString().contains("/deleteMessageByMessageId")) {
            if (session.getAttribute("adminName") != null || session.getAttribute("reader") != null) {
                filterChain.doFilter(servletRequest, servletResponse);
                return;
            }
            response.sendRedirect("http://localhost:9090/BookManage/readerLogin.html#-1");
        } else {
            if (session.getAttribute("adminName") != null) {
                filterChain.doFilter(servletRequest, servletResponse);
                return;
            }
            response.sendRedirect("http://localhost:9090/BookManage/adminLogin.html#-1");
        }
    }

}

